package com.mycompany.project.client.chee;

import com.extjs.gxt.ui.client.widget.Status;
import com.google.gwt.event.dom.client.KeyPressEvent;
import com.google.gwt.event.dom.client.KeyPressHandler;
import com.google.gwt.user.client.Timer;
import com.google.gwt.user.client.ui.AbsolutePanel;
import com.google.gwt.user.client.ui.HTML;
import com.google.gwt.user.client.ui.Image;
import com.google.gwt.user.client.ui.PasswordTextBox;
import com.google.gwt.user.client.ui.RootPanel;
import com.google.gwt.user.client.ui.TextBox;
import com.google.gwt.user.client.ui.ValueBoxBase.TextAlignment;
import com.mycompany.project.client.Main;
/**
 * Shows Login Screen GUI.
 * @author LeeNou
 *
 */
public class LoginScreen {
	/**
	 * user name.
	 */
	private String userNameStr;
	/**
	 * password.
	 */
	private String userPassword;
	/**
	 * status.
	 */
	private Status status = new Status();
	/**
	 * 1 min.
	 */
	private final static int ONEMINUTE = 1000;
	/**
	 * 3 min.
	 */
	private final static int THREEMIN = 3000;
	/**
	 * html1.
	 */
	private HTML htmlhi
	= new HTML(
			"<center><font size =\"1\">Please"
			+ "sign in.</font></center>",
			true);
	/**
	 * txtbox for password.
	 */
	private PasswordTextBox passWord = new PasswordTextBox();
	/**
	 * image.
	 */
	private Image image = new Image("images/Login2.jpg");
	/**
	 * user name textbox.
	 */
	private TextBox userName = new TextBox();
	/**
	 * the GUI.
	 */
    private AbsolutePanel login = new AbsolutePanel();
    /**
     * Constructs the login screen.
     * @param mainMCHS takes this absolutepanel and
     * add it onto the rootpanel once login is successful
     */
	public LoginScreen(final AbsolutePanel mainMCHS) {
		RootPanel.get().add(login);
		//add this login screen to root panel
		//If the user name and password are already in
		//the local storage, then use those instead.
		if (Main.getLocal() != null
				&& Main.getLocal().
				getItem("Account Saved") != null) {
			userNameStr = Main.getLocal().getItem("user name");
			userPassword = Main.getLocal().getItem("user password");
		} else {
			//default user name is Ducks
			userNameStr = "Mighty Ducks";
			//default password is 123
			userPassword = "123";
		}
		userName.setText(userNameStr);
		userName.setAlignment(TextAlignment.CENTER);
		passWord.setAlignment(TextAlignment.CENTER);
		//passWord.setText("123");
		passWord.addKeyPressHandler(new KeyPressHandler() {
			public void onKeyPress(final KeyPressEvent event) {
				if (event.getCharCode() == 13) {
					initAction(mainMCHS);
				}
			}
		});
		userName.addKeyPressHandler(new KeyPressHandler() {
			public void onKeyPress(final KeyPressEvent event) {
				if (event.getCharCode() == 13) { 
					initAction(mainMCHS);
				}
			}
		});
		login.add(passWord, 533, 255);
		passWord.setSize("143px", "16px");
		userName.setSize("143px", "16px");
		login.add(image, 360, 179);
		image.setSize("151px", "137px");
		login.add(new HTML("User Name:"),533,190);
		login.add(userName, 533, 210);
		login.add(new HTML("Password:"),533,237);
		login.add(htmlhi, 533, 289);
		htmlhi.setSize("151px", "16px");
		login.setSize("1041px", "605px");
		login.add(status);
		status.setSize("11px", "16px");
		login.setWidgetPosition(status, 533, 292);
	}
	/**
	 * GUI.
	 * @return this class's GUI.
	 */
	public final AbsolutePanel getGUI() {
		return login;
	}
	/**
	 * Sets the new user name.
	 * @param newName the user name.
	 */
	public final void setUserName(final String newName) {
		userNameStr = newName;
	}
	/**
	 * Sets the new password.
	 * @param newPass the password..
	 */
	public final void setUserPassword(final String newPass) {
		userPassword = newPass;
	}
	/**
	 * Initialized the actions for the text boxes.
	 * @param mainMCHS
	 */
	private void initAction(final AbsolutePanel mainMCHS)
	{
		userName.setEnabled(false);
		passWord.setEnabled(false);
		htmlhi.setHTML(
				"<center><font size =\"1\">Verifying identity"
				+ "...</center></font>");
		status.setIconStyle("icon-wait");
		//time 3 seconds
		 Timer t = new Timer() {
		      public void run() {
		    	  if (passWord.getText().
		    			  equals(userPassword)
		    			  && userName.getText().
		    			  equals(userNameStr)) {
		    		  htmlhi.setHTML(
		    				  "<center><font size =\"1\">"
		    				  + "Identity confirmed!</font></center>");
						status.setIconStyle(null);
						//delay 1 second again before
						//removing login screen
						Timer t = new Timer() {
						      public void run() {
						    	  //play welcome sound
						    	  //MHCSound.playIdentifyConfirm();
						    	  MHCSound.playWelcome();
						    	  RootPanel.get().
						    	  remove(login);//remove the login screen
						    	  RootPanel.get().
						    	  add(mainMCHS, 0, 0);//adds in main
						    	  htmlhi.setHTML(
						    			  "<center><font size =\"1\">Please sign in.</font></center>");
						    	  passWord.setText("");
						    	  passWord.setEnabled(true);
						    	  userName.setEnabled(true);
						      }
						    };
						    t.schedule(ONEMINUTE);
					} else {
						if (!passWord.getText().equals(userPassword)
								&& userName.getText().equals(userNameStr)) {
							htmlhi.setHTML(
									"<center><font size =\"1\">Password is incorrect!</font></center>");
							MHCSound.playInvalidPassword();
						} else if (passWord.getText().equals(userPassword)
								&& !userName.getText().equals(userNameStr)) {
							htmlhi.setHTML(
									"<center><font size =\"1\">User name is incorrect!</font></center>");
							MHCSound.playInvalidUserName();
						} else {
							htmlhi.setHTML("<center><font size =\"1\">User name and password are incorrect!</font></center>");
						}
						status.setIconStyle(null);
						passWord.setEnabled(true);
						userName.setEnabled(true);
					}
		      }
		    };
		    t.schedule(THREEMIN);
	}
}
